/*
 * @Author: your name
 * @Date: 2021-04-11 11:52:47
 * @LastEditTime: 2021-04-20 22:46:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \chronic-back-end\module\recordMod\controller.go
 */
package recordMod

import (
	"chronic/module/userMod"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type recordDto struct {
	Date           string `form:"date" json:"date" binding:"required"`
	Hospital       string `form:"hospital" json:"hospital" binding:"required"`
	PatientID      uint   `form:"patientID" json:"patientID" binding:"required"`
	PatientName    string `form:"patientName" json:"patientName" binding:"required"`
	PatientGender  string `form:"patientGender" json:"patientGender" binding:"required"`
	PatientAge     string `form:"patientAge" json:"patientAge" binding:"required"`
	PatientPhone   string `form:"patientPhone" json:"patientPhone" binding:"required"`
	Description    string `form:"description" json:"description" binding:"required"`
	DiseaseHistory string `form:"diseaseHistory" json:"diseaseHistory" binding:"required"`
	Result         string `form:"result" json:"result" binding:"required"`
	Prescription   string `form:"prescription" json:"prescription" binding:"required"`
	Remark         string `form:"remark" json:"remark" binding:"required"`
	DoctorName     string `form:"doctorName" json:"doctorName" binding:"required"`
	DoctorID       uint
}

func postOneRecord(ctx *gin.Context) {
	user := ctx.MustGet("user").(userMod.User)
	var input recordDto
	if err := ctx.ShouldBind(&input); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	input.DoctorID = user.ID
	record, err := addOneRecord(&input)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		ctx.JSON(http.StatusOK, record)
	}
}
func getRecords(ctx *gin.Context) {
	user := ctx.MustGet("user").(userMod.User)
	records, err := findRecordsByPatientId(user.ID)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		ctx.JSON(http.StatusOK, records)
	}
}

func getAllRecords(ctx *gin.Context) {
	records, err := findAllRecords()
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err})
	} else {
		ctx.JSON(http.StatusOK, records)
	}
}

func getRecordsByUserId(ctx *gin.Context) {
	id := ctx.Query("userId")
	userId, _ := strconv.ParseUint(id, 10, 64)
	records, err := findRecordsByUserId(uint(userId))
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err})
	} else {
		ctx.JSON(http.StatusOK, records)
	}
}

func getRecordsByCondition(ctx *gin.Context) {
	condition := ctx.Query("condition")
	records, err := findRecordsByCondition(condition)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err})
	} else {
		ctx.JSON(http.StatusOK, records)
	}
}

func adminDeleteRecord(ctx *gin.Context) {
	recordId := ctx.Query("id")
	id, _ := strconv.ParseUint(recordId, 10, 64)
	err := deleteRecordsById(uint(id))
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err})
	} else {
		ctx.JSON(http.StatusOK, "删除成功！！")
	}
}
